package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.Setmeal;
import com.itheima.service.OrderListService;
import com.itheima.service.SetmealService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author hrz
 * @version 1.0
 * @date 2020/6/2 16:06
 */
@RestController
@RequestMapping("/orderlist")
public class OrderListController {
    @Reference
    private OrderListService orderListService;
    @Reference
    private SetmealService setmealService;

    /**
     * 查询分页数据
     * @return 返回结果对象
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        System.out.println(queryPageBean);
        
        return orderListService.findPage(queryPageBean);
    }
    
    
    /**
     * 查询所有预约套餐,用来新建预约项时tab2的选择
     * @return 返回结果对象
     */
    @RequestMapping("/findAllSetmeal")
    public Result findAllSetmeal() {
        try {
            //套用前面的方法
            List<Setmeal> list = setmealService.findAll();
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,list);
        } catch (Exception e) {
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    //新增预约
    @RequestMapping("/add")
    public Result add(@RequestBody Map map, Integer[] setmeal_id){
        System.out.println(map);
        System.out.println(setmeal_id);
        if (setmeal_id.length >1) {
            return new Result(false,"只能选择一个检查套餐,请重新新建预约");
        }
        try {
            orderListService.addOrderAndMember(map, setmeal_id);
            return new Result(true, "新增预约成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"新增预约失败");
        }
        //System.out.println(map);
        //System.out.println(setmeal_id);
        //
        //if (setmeal_id.length >1) {
        //    return new Result(false,"只能选择一个检查套餐,请重新新建预约");
        //}
        //try{
        //    //获取前端传来的数据
        //    String name = (String) map.get("name");
        //    String idCard = (String) map.get("idCard");
        //    String birthday = (String) map.get("birthday");
        //    String phoneNumber = (String) map.get("phoneNumber");
        //    String sex = (String) map.get("sex");
        //    String orderDate = (String) map.get("orderDate");
        //    //新建预约对象并赋值
        //    Date Dateorderdate = new SimpleDateFormat("yyyy-MM-dd").parse(orderDate);
        //    Order order = new Order();
        //    order.setOrderDate(Dateorderdate);
        //    //默认将状态设为未到诊
        //    order.setOrderStatus(Order.ORDERSTATUS_NO);
        //    order.setSetmealId(setmeal_id[0]);
        //
        //    //新建会员对象并赋值
        //    Date DateBirthday = new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
        //    Member member = new Member();
        //    member.setName(name);
        //    member.setSex(sex);
        //    member.setIdCard(idCard);
        //    member.setPhoneNumber(phoneNumber);
        //    member.setBirthday(DateBirthday);
        //    
        //    //先注册会员,返回会员id
        //    orderListService.addMember(member);
        //    Integer id = member.getId();
        //    
        //    System.out.println("用户id是"+id);
        //    order.setMemberId(id);
        //    //再新增预约
        //    orderListService.addOrder(order);
        //    return new Result(true, "新增预约成功");
        //}catch (Exception e){
        //    e.printStackTrace();
        //    return new Result(false,"新增预约失败");
        //}
    }

    /**
     * 删除预约
     * @param id 预约档案号
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        Date date = orderListService.selectOrderDateById(id);
        System.out.println(date);
        
        try{
            orderListService.delete(id);
            System.out.println("给客户发短信告知"+date+"当日的预约已成功取消");
            return new Result(true, "预约取消成功");
        }catch (Exception e){
            System.out.println("给客户发短信告知"+date+"当日的预约取消失败");
            return new Result(false,"预约取消失败");
        }
    }
    /**
     * 批量删除预约
     * @param ids 预约档案号的数组
     * @return
     */
    @RequestMapping("/deleteAllSelected")
    public Result deleteAllSelected(@RequestBody String[] ids){
        System.out.println("选中的选项的id数组为:"+ids);
        
        try{
            orderListService.deleteAllSelected(ids);
            for (String id : ids) {
                Date date = orderListService.selectOrderDateById(Integer.parseInt(id));
                System.out.println("给客户发短信告知"+date+"当日的预约已成功取消");
            }
            return new Result(true, "预约批量取消成功");
        }catch (Exception e){
            for (String id : ids) {
                Date date = orderListService.selectOrderDateById(Integer.parseInt(id));
                System.out.println("给客户发短信告知"+date+"当日的预约已取消失败");
            }
            return new Result(false,"预约批量取消失败");
        }
    }

    
    /**
     * 确认预约
     * @param id 预约档案号
     * @return
     */
    @RequestMapping("/updateOrderStatus")
    public Result updateOrderStatus(Integer id){
        Date date = orderListService.selectOrderDateById(id);
        System.out.println(date);
        
        try{
            orderListService.updateOrderStatusById(id);
            System.out.println("预约状态已经改为已到诊");
            System.out.println("给客户发短信告知"+date+"当日的预约已确认");
            return new Result(true, "预约已确认");
        }catch (Exception e){
            System.out.println("给客户发短信告知"+date+"当日的预约确认失败");
            return new Result(false,"预约确认失败");
        }
    }

}
